/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : This is a KF32A156 device StdPeriph Template		  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
*                        		Include Files                                 *
******************************************************************************/
#include "system_init.h"
#include "Usart.h"
#include "Usart_Config.h"
static void Led_Gpio_Init(void);
static void Led_Flip(void);

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
static void Led_Gpio_Init(void)
{
	GPIO_Write_Mode_Bits(GPIOA_SFR, GPIO_PIN_MASK_4 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOD_SFR, GPIO_PIN_MASK_12 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOF_SFR, GPIO_PIN_MASK_7 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOH_SFR, GPIO_PIN_MASK_3 , GPIO_MODE_OUT);
}

/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
static void Led_Flip(void)
{
	GPIO_Toggle_Output_Data_Config(GPIOA_SFR, GPIO_PIN_MASK_4);
	GPIO_Toggle_Output_Data_Config(GPIOD_SFR, GPIO_PIN_MASK_12);
	GPIO_Toggle_Output_Data_Config(GPIOF_SFR, GPIO_PIN_MASK_7);
	GPIO_Toggle_Output_Data_Config(GPIOH_SFR, GPIO_PIN_MASK_3);
}

Usart_Sdu_Type  Usart_Receive_Sdu = {{0},0};
Usart_Sdu_Type  Usart_Send_Sdu = {{'C','H','I','P',' ','O','N','\r','\n'},10};
int main()
{
	uint8_t Rx_Flag = 0;
	/* Initialize the system clock */
	SystemInit(120);
	/* Setup SysTick Timer as delay function */
	systick_delay_init(120);
	/* Initialize led IOs */
	Led_Gpio_Init();
	/* Set Usart Tx And Rx PINS */
	Usart_Gpio_Init();
	/* Set Usart Working Mode  */
	USART_Mode_Config(USART0_SFR);

	while(1)
	{
		if(Rx_Flag == 1)
		{
			USART_Send(USART0_SFR,&Usart_Receive_Sdu);
			Rx_Flag = 0x00;
			USART_SendData(USART0_SFR, '\r');
			USART_SendData(USART0_SFR, '\n');
		}else
		{
			USART_Send(USART0_SFR,&Usart_Send_Sdu);
		}

		if(USART_Get_Receive_BUFR_Ready_Flag(USART0_SFR))
		{
			Usart_Receive_Sdu.Length ++;
			if( Usart_Receive_Sdu.Length >= 11 )
			{
				Usart_Receive_Sdu.Length = 1;
			}

			Usart_Receive_Sdu.Data[Usart_Receive_Sdu.Length-1] =  USART_ReceiveData(USART0_SFR);
			Rx_Flag = 0x01;
		}
		systick_delay_ms(250);
		systick_delay_ms(250);
		Led_Flip();
	}		
}
